﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;

namespace d39
{
    public abstract class GridControl : UserControl
    {
        public GridPage GridPage { get { return (GridPage)Page; } }
        public Grid CurrentGrid { get { return GridPage.CurrentGrid; } }

        public override bool EnableViewState
        {
            get { return false; }
            set { throw new InvalidOperationException(); }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            GridPage.GridUpdate += (sender, args) => UpdateFromGrid(args.PlayersOrMovesChanged, args.WallTerrainChanged);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (IsPostBack) UpdateFromGrid(true, true);
        }

        protected string GetColorString(dynamic color)
        {
            return ColorTranslator.ToHtml(color);
        }
        protected string GetContrastColorString(dynamic color)
        {
            return GetColorString(Colors.GetContrastColor(color));
        }

        protected virtual void UpdateFromGrid(bool playersOrMovesChanged, bool wallTerrainChanged)
        {
        }

        protected void GridChanged(bool playersOrMovesChanged, bool wallTerrainChanged)
        {
            GridPage.GridChanged(playersOrMovesChanged, wallTerrainChanged);
        }
    }
}